package edu.uoc.tdp.pac4.server;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import edu.uoc.tdp.pac4.beans.Aula;
import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.common.MantenimentInterface;
import edu.uoc.tdp.pac4.database.ClassroomManager;
import edu.uoc.tdp.pac4.database.CourseManager;
import edu.uoc.tdp.pac4.database.UserManager;
//import edu.uoc.tdp.pac4.database.CourseMaintManager;
//import edu.uoc.tdp.pac4.database.RoomMaintManager;

/**
 * Clase que implementa la interf�cie MantenimentInterface
 * 
 * @version 1.a; 15-11-2012; java version "1.6.0";
 * @author Enrique Quiroga Rico
 */
public class MantenimentImpl extends java.rmi.server.UnicastRemoteObject
		implements Serializable, MantenimentInterface {

	private static final long serialVersionUID = 1L;

	UserManager userManager = new UserManager();
	ClassroomManager classroomManager = new ClassroomManager();
	CourseManager courseManager = new CourseManager();

	/**
	 * Constructor de la clase<br>
	 * Crea una conexión a Base de Datos
	 * 
	 * @exception RemoteException
	 */
	public MantenimentImpl() throws RemoteException {
		super();
	}

	//USER
	
	@Override
	public List<Usuari> listUser() throws RemoteException {
		List<Usuari> users = userManager.listUser();
		return users;
	}
	
	@Override
	public List<Usuari> findUsers(Usuari usuariFilter) {
		List<Usuari> users = userManager.findUsers(usuariFilter);
		return users;
	}
	
	@Override
	public Usuari createUser(Usuari usuari) {
		System.out.println(usuari);
		if(usuari.getNacimiento()==null) {
			throw new IllegalArgumentException("birthday is null");
		}
		userManager.createUser(usuari);
		return usuari;
	}
	@Override
	public void updateUser(Usuari usuari) {
		System.out.println("updateUser "+usuari);
		if(usuari.getNIF() == null ){
			throw new IllegalArgumentException("nif is null");
		}
		userManager.updateUser(usuari);
	}
	@Override
	public Usuari findUserById(String nif) throws RemoteException {
		System.out.println(nif);
		return userManager.findUserById(nif);
	}

	

	//CLASSROOM	
	@Override
	public List<Aula> listClassroom() {
		List<Aula> classrooms = classroomManager.listClassroom();
		return classrooms;
	}
	
	@Override
	public List<Aula> findClassrooms(Aula crFilter) {
		System.out.println(crFilter);
		List<Aula> classrooms = classroomManager.findClassrooms(crFilter);
		return classrooms;
	}
	
	@Override
	public Aula createClassroom(Aula classroom) {
		System.out.println(classroom);
		if(classroom.getNom()==null) {
			throw new IllegalArgumentException("nom is null");
		}
		classroomManager.createClassroom(classroom);
		return classroom;
	}
	
	@Override
	public void updateClassroom(Aula classroom) {
		System.out.println(classroom);
		if(classroom.getIdAula()==0){
			throw new IllegalArgumentException("id is null");
		}
		classroomManager.updateClassroom(classroom);
	}
	
	@Override
	public Aula findClassroomById(Integer id) throws RemoteException {
		System.out.println(id);
		return classroomManager.findClassroomById(id);
	}
	
	
	
	//COURSE
	@Override
	public List<Course> listCourse() throws RemoteException {
		List<Course> courses = courseManager.listCourse();		
		return courses;
	}
	
	@Override
	public List<Course> findCourses(Course courseFilter) {
		System.out.println(courseFilter);
		List<Course> courses = courseManager.findCourses(courseFilter);		
		return courses;
	}
	
	@Override
	public Course createCourse(Course course) {
		System.out.println(course);
		if(course.getCourseName()==null) {
			throw new IllegalArgumentException("name is null");
		}
		courseManager.createCourse(course);
		return course;
	}
	
	@Override
	public void updateCourse(Course course) {
		courseManager.updateCourse(course);
	}
	
	@Override
	public Course findCourseById(Integer id) throws RemoteException {
		System.out.println(id);
		return courseManager.findCourseById(id);
	}
	
	
	
	
	
	
	//
	
	public Usuari findStudent(String nif){
		return userManager.findStudent(nif);
	}


	
	




	
}
